/*
 * kusabatasksendpost.cpp
 *
 *  Created on: Oct 7, 2011
 *      Author: anonovermind <anonovermind@gmail.com>
 */

#include <boost/format.hpp>
#include "kusabatasksendpost.h"

namespace Wishmaster
{
KusabaTaskSendPost::KusabaTaskSendPost(const PostDescriptor& post, const QString& captcha, Wishmaster2::Network::RequestManager* requestManager,
		const QString& msg, KusabaAgent* agent) :
	TaskSendPost(post, captcha, requestManager, msg),
	m_post(post),
	m_captcha(captcha),
	m_requestManager(requestManager),
	m_agent(agent),
	m_error(false)
{
}

//virtual void slot_result(const RequestPtr& request, const QByteArray& arr)
//{
//	log(llDebug, "KusabaTaskSendPost::slot_result");
//	// Check for errors
//	QString regexp = QString::fromUtf8("<h2 style=\"font-size: 2em;font-weight: bold;text-align: center;\">([^<>]*)</h2>");
//	QRegExp rx(regexp);
//	if(rx.indexIn(QString::fromUtf8(arr)) != -1)
//	{
//		log(llError, "Error: %s", qPrintable(rx.cap(1)));
//		m_error = true;
//		m_errorMsg = rx.cap(1);
//	}
//	TaskSendPost::slot_result(request, arr);
//}

Wishmaster2::Network::RequestPtr KusabaTaskSendPost::makeRequest(const PostDescriptor& post, const QString& captcha,
		Wishmaster2::Network::RequestManager* requestManager)
{
	log(llDebug, "KusabaTaskSendPost::makeRequest");
	QString plainboardname = post.board;
	plainboardname.remove('/');
	QString referer;
	if(post.parent_thread.threadNumericId() != 0)
		referer = QString("http://%1%2res/%3.html").arg(post.parent_thread.chanName()).
		arg(post.parent_thread.boardId()).
		arg(post.parent_thread.threadNumericId());
	else
		referer = QString("http://%1%2").arg(post.parent_thread.chanName()).
		arg(post.parent_thread.boardId());

	Wishmaster2::Network::Url url((boost::format("http://%s/board.php?dir%s") %
			post.parent_thread.chanName().toStdString() % plainboardname.toStdString()).str());
	m_request = m_requestManager->createRequest(url);
	m_request->setParameter("http:referer", referer.toStdString());
	m_request->setParameter("http:method", "POST");

	m_request->setParameter("board", std::string(plainboardname.toUtf8().constData()));
	m_request->setParameter("replythread", std::string(post.parent_thread.threadStringId().toUtf8().constData()));
	m_request->setParameter("MAX_FILE_SIZE", "1572864");
	m_request->setParameter("email", "");
	if(post.sage)
		m_request->setParameter("em", "sage");
	m_request->setParameter("subject", std::string(post.topic.toUtf8().constData()));
	m_request->setParameter("message", std::string(post.text.toUtf8().constData()));
	m_agent->addAttachments(m_request, post.attachments.count() > 0 ? post.attachments.at(0) : AttachmentDescriptor(), "imagefile");
	m_request->setParameter("redirecttothread", "1");
	m_request->setParameter("postpassword", std::string(post.password.toUtf8().constData()));
	m_request->setParameter("captcha", std::string(captcha.toUtf8().constData()));

	return m_request;
}

void KusabaTaskSendPost::onRequestDone()
{
	QByteArray arr(m_request->content()->data(), m_request->content()->size());
	emit result(this, arr, QString(m_request->contentType().c_str()));
}

}
